package tt.dz.biz.service.financial;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.financial.MerchantReportDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.BorrowLoanCheckVO;
import tt.dz.vo.MerchAccountDetailVO;
import tt.dz.vo.MerchantSubjectAccVO;
import tt.dz.vo.SubjectVO;
import tt.dz.vo.TranInfoVO;

@Service("merchantReportService")
public class MerchantReportService
{
  @Resource(name="merchantReportDAO")
  private MerchantReportDAO merchantReportDAO;
  @Resource(name="subjectService")
  private SubjectService ubjectService;
  
  public List<MerchAccountDetailVO> querymerchantDailyAccouting(MerchAccountDetailVO param)
  {
    try
    {
      return this.merchantReportDAO.queryMerchantDailyAccouting(param);
    }
    catch (Exception e)
    {
      LogUtil.error("查询日记账异常，参数为：param=" + param, e);
    }
    return new ArrayList();
  }
  
  public List<BorrowLoanCheckVO> queryBorrowLoanCheck(Date startDate, Date endDate)
  {
    try
    {
      return this.merchantReportDAO.queryBorrowLoanCheck(startDate, endDate);
    }
    catch (Exception e)
    {
      DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
      LogUtil.error("查询借贷平衡异常，参数为：startDate=" + ft.format(startDate) + ",endDate=" + ft.format(endDate), e);
    }
    return new ArrayList();
  }
  
  public List<TranInfoVO> queryChannelPayList(String channelCode, Date startDate, Date endDate)
    throws Exception
  {
    try
    {
      return this.merchantReportDAO.queryChannelPayList(channelCode, startDate, endDate);
    }
    catch (Exception e)
    {
      DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
      LogUtil.error("查询渠道交易明细异常，参数为：channelCode=" + channelCode + ",startDate=" + ft.format(startDate) + ",endDate=" + ft.format(endDate), e);
      throw e;
    }
  }
  
  public List<Map<String, Object>> queryBalanceOfPayments(Date startDate, Date endDate, String merchantCode)
    throws Exception
  {
    try
    {
      return this.merchantReportDAO.queryBalanceOfPayments(startDate, endDate, merchantCode);
    }
    catch (Exception e)
    {
      DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
      String start = null;
      String end = null;
      if (null != startDate) {
        start = ft.format(startDate);
      }
      if (null != endDate) {
        end = ft.format(endDate);
      }
      LogUtil.error("查询收支明细异常，参数为：merchantCode=" + merchantCode + ",startDate=" + start + ",endDate=" + end, e);
      throw e;
    }
  }
  
  public List<MerchantSubjectAccVO> queryMerchantSubjectAcc(MerchantSubjectAccVO param)
    throws Exception
  {
    try
    {
      if (StringUtils.isNotBlank(param.getSubSubjectCode()))
      {
        SubjectVO subject = this.ubjectService.querySubjectByCode(param.getSubSubjectCode());
        if (null == subject) {
          return this.merchantReportDAO.queryMerchantSubjectAcc(param);
        }
        if ("#".equals(subject.getParentSubjectCode())) {
          return this.merchantReportDAO.queryMerchantAccForFirstLevelSubject(param);
        }
        return this.merchantReportDAO.queryMerchantSubjectAcc(param);
      }
      return this.merchantReportDAO.queryMerchantSubjectAcc(param);
    }
    catch (Exception e)
    {
      LogUtil.error("查询企业账户信息异常，参数为：param=" + param, e);
      throw e;
    }
  }
}
